package com.ubtechinc.cruzr.launcher2.model;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;

import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.model.helper.ThirdStateHelper;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.sdk.status.StatusInitCallBack;
import com.ubtechinc.cruzr.sdk.status.SystemStatusApi;
import com.ubtechinc.cruzr.serverlibutil.aidl.StatusInfo;
import com.ubtechinc.cruzr.serverlibutil.interfaces.RemoteStatusListener;

import java.util.List;

public class StatusManager {
    private ValueAnimator mDeleteVA;
    private boolean isAiniting = false;
    private float mCurrentFloat;
    private boolean isInit = false;

    StatusManager(Context context){
        SystemStatusApi.get().init(context, new StatusInitCallBack() {
            @Override
            public void onInit() {
                super.onInit();
                initCallBack();
                isInit = true;
            }

            @Override
            public void onReConnect() {
                super.onReConnect();
                initCallBack();
            }
        });
    }
    private void initCallBack(){
        SystemStatusApi.get().registerStatusCallback(new RemoteStatusListener() {
            @Override
            public void onResult(List<Integer> list, boolean b, int i) {

            }

            @Override
            public void onStatusPause(int i, int i1, String s) {
                LogUtils.e("onStatusPause remove third app status");
                ThirdStateHelper.removeThirdAppTask(LauncherApplication.getContext());
                removeThirdAppStatus();
            }

            @Override
            public void onStatusFree(int i) {

            }
        });
    }
    public void setThirdAppStatus(){
        if(isInit){
            ThirdStateHelper.setThirdAppState();
            SystemStatusApi.get().setAppStatus(ThirdStateHelper.THIRD_APP_STATUS,true);
        }
    }
    public void removeThirdAppStatus(){
        if(isInit){
            SystemStatusApi.get().removeAppStatus(ThirdStateHelper.THIRD_APP_STATUS);
        }
    }
    public boolean isContainsThirdAppStatus(){
        if(!isInit){
            return false;
        }
        List<StatusInfo> status = SystemStatusApi.get().getCurrentStatusList();
        if (status != null && !status.isEmpty()) {
            for (StatusInfo info : status) {
                if (info.status == ThirdStateHelper.THIRD_APP_STATUS) {
                    return true;
                }
            }
        }
        return false;
    }
    public void startDeleteAinimation() {
        if (isAiniting) {
            return;
        }
        if (mDeleteVA == null) {
            mDeleteVA = ValueAnimator.ofFloat(1f, 0.95f, 1f);
        }
        mDeleteVA.setRepeatCount(ValueAnimator.INFINITE);
        mDeleteVA.setInterpolator(new TimeInterpolator() {

            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        mDeleteVA.setDuration(1000);
        mDeleteVA.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentFloat = ((Float) animation.getAnimatedValue());
            }
        });
        mDeleteVA.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            public void onAnimationRepeat(Animator animation) {

            }
        });
        isAiniting = true;
        mDeleteVA.start();
    }
    public void stopDeleteAinimation(){
        if (mDeleteVA != null) {
            mDeleteVA.cancel();
        }
        isAiniting = false;
        mCurrentFloat = 1f;
    }
    public float getCurrentFloat() {
        return mCurrentFloat;
    }
}
